﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Text;

namespace Music.Utility.Baidu.Face
{
    /// <summary>
    /// Baidu人脸识别
    /// </summary>
    public class FaceHelper
    {
        /// <summary>
        /// 校验姓名及身份证号是否输入正确：根据error_code判断，为0时表示匹配为同一个人。否则按错误码表的定义，如222351表示身份证号码与名字不匹配。
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id_card_number"></param>
        /// <returns></returns>
        public static bool CheckNameAndID(string name, string id_card_number)
        {
            bool bValue = false;
            //var access_token = AccessToken.getAccessToken("VeZKepIYOVLC35s3qAR36HCU", "neK2VOtxQ9Y0kDt5G70hXztGyMzBIPIj");
            var access_token = AccessToken.getAccessToken("iRIdALDtV7fBUsZ3Q5TX7KS8", "YIh2NhqO4Hrg4D8u5ZRuwz8jlWAaExnU");
            JObject obj = (JObject)JsonConvert.DeserializeObject(access_token);
            access_token = obj["access_token"].ToString();

            string url = "https://aip.baidubce.com/rest/2.0/face/v3/person/idmatch?access_token="+ access_token;

            var client = new RestClient(url);
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddParameter("id_card_number", id_card_number);
            request.AddParameter("name", name);
            IRestResponse attachmentResponse = client.Execute(request);
            var message = attachmentResponse.Content;

            //{"error_code":0,"error_msg":"SUCCESS","log_id":7579654584942,"timestamp":1626131931,"cached":0,"result":null}
            JObject obj2 = (JObject)JsonConvert.DeserializeObject(message);
            var error_code = obj2["error_code"].ToString();
            var error_msg = obj2["error_msg"].ToString();
            if (error_code =="0" && error_msg== "SUCCESS")
            {
                bValue = true;
            }

            return bValue;
        }


        /// <summary>
        /// 勺海科技:校验姓名及身份证号是否输入正确：根据error_code判断，为0时表示匹配为同一个人。否则按错误码表的定义，如222351表示身份证号码与名字不匹配。
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id_card_number"></param>
        /// <returns></returns>
        public static bool CheckNameAndIDForShaoHai(string name, string id_card_number)
        {
            bool bValue = false;
            var access_token = AccessToken.getAccessToken("dlhs20210422", "50475646554F5252555743414E505145");
            JObject obj = (JObject)JsonConvert.DeserializeObject(access_token);
            access_token = obj["access_token"].ToString();

            string url = "http://180.76.104.246:8080/data/verify?access_token=" + access_token;

            var client = new RestClient(url);
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddParameter("id_card_number", id_card_number);
            request.AddParameter("name", name);
            IRestResponse attachmentResponse = client.Execute(request);
            var message = attachmentResponse.Content;

            //{"error_code":0,"error_msg":"SUCCESS","log_id":7579654584942,"timestamp":1626131931,"cached":0,"result":null}
            JObject obj2 = (JObject)JsonConvert.DeserializeObject(message);
            var error_code = obj2["error_code"].ToString();
            var error_msg = obj2["error_msg"].ToString();
            if (error_code == "0" && error_msg == "SUCCESS")
            {
                bValue = true;
            }

            return bValue;
        }
    }
}
